package com.yonyou.pmclouds.workflow.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.billtype.entity.BillTypeVO;
import com.yonyou.pmclouds.workflow.entity.ProcessDataSynResultVO;
import com.yonyou.pmclouds.workflow.entity.ProcessDataSynVO;
import com.yonyou.pmclouds.workflow.entity.UserAndUserGroupVO;
import com.yonyou.pmclouds.workflow.mapper.ProcessDataSynMapper;
import com.yonyou.pmclouds.workflow.service.BaseDataSynService;
import com.yonyou.pmclouds.workflow.service.FormSynService;
import com.yonyou.pmclouds.workflow.service.rmiitf.BaseManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import yonyou.bpm.rest.exception.RestException;

import java.util.List;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = BaseManagerService.class, timeout = 4000)
public class BaseManageServiceImpl implements BaseManagerService {

    @Autowired
    private ProcessDataSynMapper synMapper;

    @Autowired
    private BaseDataSynService baseDataSynService;

    @Autowired
    private FormSynService formSynService;

    @Override
    public void insertBaseDataSyn(List<ProcessDataSynVO> synVOList) throws BusinessException {
        if(synVOList != null && synVOList.size() > 0) {
            MultiInsertBpTemplate<ProcessDataSynVO> mbpTemplate = new MultiInsertBpTemplate<>(synMapper);
            mbpTemplate.insert(synVOList.toArray(new ProcessDataSynVO[synVOList.size()]));
        }
    }

    @Override
    public ProcessDataSynVO queryLastSynInfo(String tenantId) throws BusinessException {
        return synMapper.queryLastSynInfo(tenantId);
    }

    @Override
    public ProcessDataSynVO checkIsBaseDataSyn(String tenantId) throws BusinessException {
        return synMapper.checkIsBaseDataSyn(tenantId);
    }

    @Override
    public String addTenantDataSyn(String tenantInfo, String userId, String tenantId, String sysId) throws BusinessException {
        try {
            ProcessDataSynResultVO resultVO = baseDataSynService.tenantDataSyn(tenantInfo, userId, tenantId, sysId);
            this.insertBaseDataSyn(resultVO.getDataSynVOList());
            return resultVO.getSynResult();
        } catch (RestException e) {
            throw new BusinessException("同步租户信息至云审批 : " + e.getMessage());
        }
    }

    @Override
    public String addRoleDataSyn(List<UserAndUserGroupVO> userGroupVOList, String userId, String tenantId) throws BusinessException {

        try {
            ProcessDataSynResultVO resultVO = baseDataSynService.roleDataSyn(userGroupVOList, userId, tenantId);
            this.insertBaseDataSyn(resultVO.getDataSynVOList());
            return resultVO.getSynResult();
        } catch (RestException e) {
            throw new BusinessException("同步角色信息至云审批 : " + e.getMessage());
        }

    }

    @Override
    public String addCallBackListener(String userId, String tenantId) throws BusinessException {
        ProcessDataSynResultVO resultVO = baseDataSynService.registerCallBackListener(userId, tenantId);
        this.insertBaseDataSyn(resultVO.getDataSynVOList());
        return resultVO.getSynResult();
    }

    @Override
    public void addFormDataSyn(String processModelId, BillTypeVO billTypeVO, String userId, String tenantId) throws BusinessException{
        ProcessDataSynResultVO resultVO = formSynService.formDataSyn(processModelId, billTypeVO, userId, tenantId);
        this.insertBaseDataSyn(resultVO.getDataSynVOList());
    }

    @Override
    public void deleteFormData(String processModelId, String userId, String tenantId) throws BusinessException{
        formSynService.deleteFormAndField(processModelId, userId, tenantId);
    }

    @Override
    public String addDataSyn(String tenantInfo, String userId, String tenantId, String sysId) throws BusinessException {
        return this.addTenantDataSyn(tenantInfo, userId, tenantId, sysId) +
                this.addCallBackListener(userId, tenantId);
    }
}
